package com.my.study.juc.basic;

/**
 * 不能保证原子性
 *
 * @author: yidujun
 * @create: 2021/04/12 16:54
 */
public class VolatileTest {

    /**
     * 锁
     */
    public static final Object obj = new Object();

    public volatile int inc = 0;

    public void increase() {
        inc++;
    }

    public static void main(String[] args) {
        final VolatileTest test = new VolatileTest();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    test.increase();
                }
            }).start();
        }

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(test.inc);
    }

}
